﻿using System;
using System.Web;
using System.Collections.Generic;
using WAD.WEB.AppCode;
using WAD.Model;
using WAD.DAL;

namespace WAD.WEB.Client.Ajax
{
    /// <summary>
    /// Article 的摘要说明
    /// </summary>
    public class Article : AjaxBase
    {
        public override void AjaxMethodDispatch(String identity)
        {
            switch (identity)
            {
                case "add": this.Add(); break;
                case "update": this.Update(); break;
                case "click": this.Click(); break;
                case "getdetail": this.GetDetail(); break;
                case "remove": this.Remove(); break;
                case "getlist": this.GetList(); break;
                case "getlistindex": this.GetListIndex(); break;
            }
        }
        /// <summary>
        /// 添加
        /// </summary>
        [QD.Web.Ajax.AjaxMethod]
        public void Add()
        {
            IsLogin();

            String title = Parse.String("title");//标题
            String content = Parse.String("content");//类容
            String logo = Parse.String("logo");//标题logo
            String keyWord = Parse.String("key_word");//标题logo
            bool is_recommend = Parse.Boolean("is_recommend", true);//是否允许推荐

            //验证
            if (String.IsNullOrEmpty(title) || String.IsNullOrEmpty(content) || String.IsNullOrEmpty(logo))
            {
                ParamError();
            }

            content = HttpUtility.UrlDecode(content);

            //关键词为空时 赋值为标题值
            if (String.IsNullOrEmpty(keyWord))
            {
                keyWord = title;
            }

            MArticle m = new MArticle() { content = content, logo = logo, title = title, user_id = UserId, key_word = keyWord, is_recommend = is_recommend };
            int id = DaoArticle.Add(m, true);
            if (id > 0)
                Success(id);
            else
                Fail();
        }
        /// <summary>
        /// 添加
        /// </summary>
        [QD.Web.Ajax.AjaxMethod]
        public void Update()
        {
            IsLogin();

            String title = Parse.String("title");//标题
            int articleId = Parse.Int("article_id", 0);//标题
            String content = Parse.String("content");//类容
            String logo = Parse.String("logo");//标题logo
            String keyWord = Parse.String("key_word");//标题logo
            bool is_recommend = Parse.Boolean("is_recommend", true);//是否允许推荐

            //验证
            if (String.IsNullOrEmpty(title) || String.IsNullOrEmpty(content) || String.IsNullOrEmpty(logo) || articleId == 0)
            {
                ParamError();
            }

            content = HttpUtility.UrlDecode(content);

            //关键词为空时 赋值为标题值
            if (String.IsNullOrEmpty(keyWord))
            {
                keyWord = title;
            }

            MArticle m = new MArticle() { content = content, logo = logo, title = title, user_id = UserId, key_word = keyWord, is_recommend = is_recommend, id = articleId };
            bool flag = DaoArticle.Update(m);
            if (flag)
            {
                Success();
            }
            else
            {
                Fail();
            }
        }

        /// <summary>
        /// 增加阅读数
        /// </summary>
        [QD.Web.Ajax.AjaxMethod]
        public void Click()
        {
            int id = Parse.Int("id", 0);
            int number = Parse.Int("number", 1);
            if (number < 1)
                number = 1;
            if (id > 0)
                DaoArticle.UpdateClick(id, number);
            JsonResult(0, "");
        }

        /// <summary>
        /// 获取文章详情
        /// </summary>
        [QD.Web.Ajax.AjaxMethod]
        public void GetDetail()
        {
            int id = Parse.Int("id", 0);

            if (id == 0)
            {
                ParamError();
            }

            Dictionary<string, object> map = DaoArticle.GetDetail(id);
            if (map != null)
            {
                DaoArticle.UpdateClick(id);
                Success(map);
            }
            else
            {
                Fail();
            }
        }

        /// <summary>
        /// 移除一条原创
        /// </summary>
        [QD.Web.Ajax.AjaxMethod]
        public void Remove()
        {
            IsLogin();
            int id = Parse.Int("id", 0);

            if (id == 0)
            {
                ParamError();
            }

            if (DaoArticle.Hide(id, UserId))
            {
                Success();
            }
            else
            {
                Fail();
            }

        }

        /// <summary>
        /// 获取文章列表
        /// </summary>
        [QD.Web.Ajax.AjaxMethod]
        public void GetList()
        {
            String keyWord = Parse.String("key_word");//搜索的关键词
            int channelId = Parse.Int("channel_id", 0);//获取频道
            int authorId = Parse.Int("author_id", 0);//作者id
            int isRecommend = Parse.Int("is_recommend", -1);

            MArticle m = new MArticle() { channel_id = channelId, key_word = keyWord, user_id = authorId };

            if (isRecommend != -1)
            {
                m.is_recommend = isRecommend == 1;
            }

            int pageIndex = Parse.Int("page_index", 1);//当前页

            PageData pg = DaoArticle.GetList(pageIndex, 20, m, "user_id");

            if (pg.itemCount == 0)
            {
                DataEmpty();
            }
            else
            {
                Success(pg);
            }
        }

        [QD.Web.Ajax.AjaxMethod]
        public void GetListIndex()
        {
            int pageIndex = Parse.Int("page_index", 1);//当前页
            int pageSize = 20;
            JsonList(DAO.GetListPage("*", "id desc", pageIndex, pageSize, "", "v_shared#article##index"));
        }
    }
}